/*
 *  tableMgr.h
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 5. 1..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */

#ifndef _KD_TABLE_MGR_H_
#define _KD_TABLE_MGR_H_ (1)

#include "kdInfra.h"
#include "types.h"
#include "table.h"
#include "arrayMap.h"
#include "sharedRegion.h"

/** @brief Maintain multiple stgTable objects. Provide shared memory chunks for storing (key,data) pairs of tables. 
 * The shared memory chunks are from a dedicated stgSharedRegion object.
 * Note : stgTableMgr is used both for normal processing and restart recovery. So, it does not have any locking nor logging code.
 */
class stgTableMgr
{
private :
	/** @brief Keeps mappings from integer table ID(stgTableId) to the class that handles table operations(stgTable*)
	 */
	kdArrayMap<stgTableId, stgTable*> map_;
	
	/** @brief A shared memory region that manages shared memory chunks for tables.
	 * A table can have at least a shared memory chunk. 
	 * All the data structure related to access methods on the key and its associated data are stored in the shared memory chunk.
	 * Why shared memory? 
	 *   To boost restart recovery time upon process crash. 
	 *   KangmoDB can rollback uncomitted data in the shared memory by undoing logs stored in transaction log buffer in another shared memory region.
	 */
	stgSharedRegion region_;
public :
	
	stgTableMgr()
	{
	}
	
	~stgTableMgr()
	{
	}

	/** @brief Initialize the table manager. Support up to maxTableCount
 	*/
	KD_VOID initialize(const KD_STRING & regionName, void * mappedAddr, u32 sharedChunkSize, u32 maxChunkCount, u32 maxTableCount )
	{
		KD_TRY
		{
			KD_CALL( region_.openRegion( regionName, mappedAddr, sharedChunkSize, maxChunkCount ) );
			KD_CALL( map_.initialize( maxTableCount ) );
		}
		KD_CATCH
		KD_FINALLY
		KD_END
	}

	/** @brief Destroy the table manager.
 	*/
	KD_VOID destroy()
	{
		KD_TRY
		{
			// TODO : memory leak : destroy each stgTable object in the map.
			KD_CALL( map_.destroy() );
			KD_CALL( region_.closeRegion() );
		}
		KD_CATCH
		KD_FINALLY
		KD_END
	}

	/** @brief Allocate a new table in the table manager with the given tableId.
	 * Called by the time we redo CREATE_TABLE log during the restart recovery. The tableId is from the CREATE_TABLE log.
	 */
	KD_VOID newTable(stgTableId tableId)
	{
		KD_TRY
		{
			// TODO : Implement
			KD_ASSERT(0);
		}
		KD_CATCH
		{
			KD_CONVERT(KD_EXCP_ARRAY_MAP_FULL, KD_EXCP_HIT_MAX_TABLE_COUNT);
		}
		KD_FINALLY
		KD_END
	}

	/** @brief Allocate a new table in the table manager with the given tableId.
	 * Called when a new table is created with Kangmodb::createTable interface.
	 */
	KD_VOID newTable(stgTableId * tableId)
	{
		KD_TRY
		{
			// TODO : Implement
			KD_ASSERT(0);
		}
		KD_CATCH
		{
			KD_CONVERT(KD_EXCP_ARRAY_MAP_FULL, KD_EXCP_HIT_MAX_TABLE_COUNT);
		}
		KD_FINALLY
		KD_END
	}

	/** @brief Delete a table from the table manager.
	 * 1) Called by the time we redo DROP_TABLE log during the restart recovery. The tableId is from the CREATE_TABLE log.
	 * 2) Called when a table is dropped with Kangmodb::dropTable interface.
	 */
	KD_VOID deleteTable(stgTableId tableId)
	{
		KD_TRY
		{
			// TODO : Implement
			KD_ASSERT(0);
		}
		KD_CATCH
		KD_FINALLY
		KD_END
	}
	
	/** @brief Get a table from the table manager with the given tableId.
	 */
	KD_VOID getTable(stgTableId tableId, stgTable ** table)
	{
		KD_TRY
		{
			// TODO : Implement
			KD_ASSERT(0);
		}
		KD_CATCH
		KD_FINALLY
		KD_END
	}
};

#endif /* _KD_TABLE_MGR_H_ */
